package vn.fpt.fsoft.filehandle;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.text.SimpleDateFormat;

import java.util.Date;

/**
 * This class will handler with files and directories : such as copying, moving,
 * deleting.
 * 
 * @author DinhTV
 * 
 */
public class FileHandler {

	private String destinationPath;
	private String dir;

	public String getDir() {
		return dir;
	}

	public void setDir(String dir) {
		this.dir = dir;
	}

	public String getDestinationPath() {
		return destinationPath;
	}

	public void setDestinationPath(String destinationPath) {
		this.destinationPath = destinationPath;
	}

	/**
	 * convert current date to String for making file and folder name by date.
	 * 
	 * @return (String) current date string.
	 */
	public String toStringCurrentDate() {
		String currentDate = null;
		Date date = new Date();
		SimpleDateFormat sp = new SimpleDateFormat("MM-dd-yyyy");
		currentDate = sp.format(date).toString();
		return currentDate;
	}

	/**
	 * make file name with current name plus suffix ,that is modify datetime.
	 * 
	 * @param file
	 *            current file to be renamed with date suffix
	 * @return the renamed file'name
	 */
	public String makeFileName(File file) {
		String fileName = null;
		char dot = '.';
		String dateFormat = "MM-dd-yyyy";
		int lastDot = 0;
		fileName = file.getName();
		lastDot = fileName.lastIndexOf(dot);
		String subFileName = fileName.substring(0, lastDot);
		Date date = new Date();

		SimpleDateFormat sp = new SimpleDateFormat(dateFormat);
		System.out.println(file.lastModified());
		fileName = subFileName + "--" + sp.format(date).toString();

		return fileName;
	}

	/**
	 * get extension( type) of file
	 * 
	 * @param file
	 *            - the file will be gotten the extension.
	 * 
	 * @return extension of file with dot (".") character
	 * @throws IOException
	 */
	public String getFileExtension(File file) throws IOException {
		String fileName = null;
		String extension = null;
		fileName = file.getCanonicalPath();
		// get last dot character in full path of file.
		int lastDot = fileName.lastIndexOf(".");

		// get last string after dot(consist of extension of file)
		extension = fileName.substring(lastDot);
		return extension;
	}

	/**
	 * 
	 * @param source
	 *            - the source file to copy to destination file
	 * @param destination
	 *            - the destination file
	 * @return true if copy the file success <br>
	 *         false if an error was occurred or the source file was not found.
	 * @throws IOException
	 */
	public boolean copyFile(File sourceFile, File destinationFile)
			throws IOException {

		boolean result = false;
		FileChannel inputChannel = null;
		FileChannel outputChannel = null;
		try {
			inputChannel = new FileInputStream(sourceFile).getChannel();
			outputChannel = new FileOutputStream(destinationFile).getChannel();
			outputChannel.transferFrom(inputChannel, 0, inputChannel.size());
			result = true;
		} finally {
			if (inputChannel != null) {
				inputChannel.close();
			}
			if (outputChannel != null) {
				outputChannel.close();
			}
		}
		return result;
	}

	/**
	 * Delete a file
	 * 
	 * @param file
	 *            -the file will be deleted
	 * @return true if the file was deleted <br>
	 *         false if the file was not deleted.
	 */
	public boolean deleteFile(File file) {
		boolean result = false;
		if (file.delete()) {
			result = true;
		}
		return result;
	}

	/**
	 * Scan all files in the sources directory and copy file to destination
	 * folder and backup folder
	 * 
	 * @param sourceDirectory
	 * @return
	 * @throws IOException
	 */
	public boolean FilesScanner(File sourceDirectory) throws IOException {
		boolean result = false;
		String extension = null;
		String destinationBackupFileName = null;
		File[] files = sourceDirectory.listFiles();
		for (File file : files) {
			if (file.isDirectory()) {
				FilesScanner(file);
			} else {
				extension = this.getFileExtension(file);
				// make the file uri for back kup
				destinationBackupFileName = this.getDestinationPath()
						+ this.makeFileName(file) + extension;
				File destinationFile = new File(destinationBackupFileName);

				this.copyFile(file, destinationFile);
			}
		}
		return result;
	}

	// main method for test.
	public static void demo() {
		FileHandler test = new FileHandler();
		test.setDestinationPath("F:/Temp/destination/files/other/09-12-2014/");

		try {
			test.FilesScanner(new File("F:/Temp/Share"));
		} catch (IOException e) {
			e.printStackTrace();
		}

	}
}
